<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-13
 * Time: 2:04
 */

namespace app\api\controller\v1;
use app\common\logic\MallLogic;
use app\common\controller\Api;
use app\common\logic\GoodsLogic;
use app\common\logic\Logic;
use app\common\model\CatType;
class Goods extends Api
{
    protected $authenticate = ['only'=>'ff'];
    
    public function index(){
        $params = paramForm();
        //如果有筛选字段，就加上status
        if(isset($params['field'])){
            $params['field'].=',status';
        }
        
        if(isset($params['action'])){
            switch ($params['action']){
                case 'cat':
                    $types = CatType::all(['category_id'=>paramForm('catid',true)]);
                    $typeIds=[];
                    foreach ($types as $type){
                        $typeIds[] = $type->type_id;
                    }
                    if(count($typeIds)<1){
                        error('该菜单未关联叶子类目');
                    }
                    
                    $logic = new GoodsLogic();
                    $params['paginate'] = 1;
                    $params['status'] = 3;
                    $params['type_id'] = array('in',$typeIds);
                    $lists = $logic->getList($params);
                    success($lists);
                    break;
                default:
                    break;
            }
            die;
        }
    
    
        $params['status'] = 3;
    
        //如果有id参数，则表示获取单个
        if(isset($params['id'])){
            $this->read($params);
            die;
        }
    
        $logicClass = $this->logicClass;
        $logic = new $logicClass();

        $lists = $logic->getList($params);
        if($lists){
            success($lists);
        }else{
            error('没有符合的数据');
        }
        
        
    }

    
    public function read($params)
    {
        
        $logic = new GoodsLogic();
        $logic->setSql($params);
        $goods = $logic->findModel();
       
        if($goods->status!=3){
            api(0,'该商品已经下架',['errorCode'=>10001,'errorMsg'=>'该商品下架']);
        }
        //简单模式的信息
        if(isset($params['easy'])&&$params['easy']){
            success($goods);
        }
        $goods->attr;
        $goods->company;
        $goods->city;
        
        $goods->favorite = $logic->getFavorite($goods->id,$this->getUserId());
    
        $goods->fare = $logic->getFare($goods->fare_id);
       
        $groups = $logic->getGroups($goods->id);
        
        //偷天换日，用团购换正式价格
        if($groups){
            
            $tempArr = [];
            $mallLogic = new MallLogic();
            $goods->stockTotal = 0;
            $groups->info = $groups->getInfo();//排好序的
            foreach ($groups->info as $info){
                
                $mall = $mallLogic->findModel($info->mall_id);
                $mall->specs;
                //替换掉库存和价格。。。
                $mall->price = $info->price;
                $mall->stock = $info->stock;
    
                $tempArr[] = $mall;
                
                $goods->stockTotal+=$mall->stock;
            }
            unset($groups->info);
            
            $goods->mall = $tempArr;
            
        }else{
            $goods->stockTotal = 0;
            $goods->mall = $goods->getMall();//排好序的
            foreach ($goods->mall as $mall){
                $mall->specs;
                $goods->stockTotal+=$mall->stock;
            }
            
        }
        
        $goods->group = $groups;
        
        $specAllArr = [];
        foreach ($goods->spec as $spec){
            $specAllArr[$spec->spec_id][] = ['spec_id'=>$spec->spec_id,'val'=>$spec->val,'title'=>$spec->title];
        }
        
        unset($goods->spec);

        $rtSpecs = [];
        foreach ($specAllArr as $key=>$oneSpecArr){
            $tempArr = array_unique($oneSpecArr,SORT_REGULAR);

            foreach ($tempArr as $item){
                $rtSpecs[$key][]=$item;
            }
        }
        
        $goods->specOptions = $rtSpecs;//array_unique_fb($specs);
        
        if($goods){
            success($goods);
        }else{
            error('数据不存在或者无权限查看');
        }
        
    }


    
}